package server.content.skills.fishing;

import core.util.Misc;
 
import server.content.skilling.PlayerSkill;
import server.event.CycleEvent;
import server.event.CycleEventContainer;
import server.event.CycleEventHandler;
import server.game.players.Client;

/**
 * 
 * @author Miracle
 * 
 */
public class CageFishing extends PlayerSkill {

	private final int RAW_LOBSTER = 377;
	private final int CAGE_ANIM = 619;
	private final int LOBSTER_POT = 301;

	public CageFishing(final Client c, final int skillId) {
		super(c, skillId);
		setTick(2);
	}

	public void init() {
		if (getClient().getItems().freeSlots() < 1) {
			getClient().sendMessage(
					"You're inventory is to full to fish any more Lobsters.");
			kill();
			return;
		}
		if (getPlayerLevel() < 40) {
			getClient().sendMessage(
					"You need a fishing level of 40 to catch Lobsters..");
			kill();
			return;
		}
		if (!getClient().getItems().playerHasItem(LOBSTER_POT)) {
			getClient().sendMessage("You need a Lobster Pot to fish here.");
			kill();
			return;
		}
		if (startTime < 0)
			startTime = System.currentTimeMillis();
		setActive(true);
		doAnimation(CAGE_ANIM);
		process();
	}

	public void process() {
		CycleEventHandler.getSingleton().addEvent(getClient(),
				new CycleEvent() {

					@Override
					public void execute(CycleEventContainer container) {
						if (isActive()) {
							if (getClient().getItems().freeSlots() < 1) {
								kill();
								getClient()
										.sendMessage(
												"You're inventory is to full to fish any more Lobsters.");
								return;
							}
							if (getPlayerLevel() < 40) {
								getClient()
										.sendMessage(
												"You need a fishing level of 40 to catch Lobsters..");
								kill();
								return;
							}
							if (!getClient().getItems().playerHasItem(
									LOBSTER_POT)) {
								getClient().sendMessage(
										"You need a Lobster Pot to fish here.");
								kill();
								return;
							}
							doAnimation(CAGE_ANIM);
							if (Misc.random(100) <= getChanceForLevel()) {
								addItem(RAW_LOBSTER, 1);
								getClient().sendMessage(
										"You manage to fish a lobster.");
								addXP(260, 10);
							}
						} else {
							container.stop();
						}
					}

					@Override
					public void stop() {

					}
				}, 2);
	}

	public int getChanceForLevel() {
		final int lvl = getPlayerLevel();
		if (lvl > 39 && lvl < 51) {
			return 7;
		} else if (lvl > 50 && lvl < 61) {
			return 15;
		} else if (lvl > 60 && lvl < 71) {
			return 25;
		} else if (lvl > 70 && lvl < 81) {
			return 34;
		} else if (lvl > 80 && lvl < 99) {
			return 47;
		}
		return 10;
	}

	public void kill() {
		resetAnim();
		setActive(false);
	}

}
